package com.bw.day_03.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.bw.day_03.bean.Tb_brand;
import com.bw.day_03.bean.Tb_goods;
import com.bw.day_03.bean.Tb_goodskind;
import com.bw.day_03.service.ServiceL;
import com.github.pagehelper.PageInfo;

@Controller
public class GoodContorller {

	@Autowired
	private ServiceL service;
	
	@RequestMapping("list")
	public String listGood(Model model,@RequestParam(defaultValue = "1")Integer pageNum) {
		int pageSize=3;
		System.out.println(pageNum);
		PageInfo<Tb_goods> page=service.list1(pageNum,pageSize);
		model.addAttribute("pg", page);
		return "list";
	}
	
	@RequestMapping("selectxiang")
	public String selectxiang(Model model,String id) {
		model.addAttribute("id", id);
		return "xiang";
	}
	
	@RequestMapping("selectxiang1")
	@ResponseBody 
	public Tb_goods selectxiang1(String id) {
		Tb_goods good=service.selectxiang(id);
		return good;
	}
	
	@RequestMapping("updategood")
	public String updategood(Model model,String id) {
		model.addAttribute("id", id);
		return "update";
	}
	
	@RequestMapping("addgood")
	public String addgood() {
		return "add";
	}
	
	

	 @RequestMapping("add") 
	 public String addgood(Model model,MultipartFile myFile,Tb_goods good,String pid,HttpServletRequest request) throws IllegalStateException, IOException{
		 if(myFile!=null && myFile.getSize()>0) {
			 String real =request.getServletContext().getRealPath("/img/");
			 String ori = myFile.getOriginalFilename();
			 ori=UUID.randomUUID().toString()+ori;
			 File file = new File(real, ori);
			 myFile.transferTo(file);
			 good.setTu(ori);
		}
		boolean aa=service.add(good);
		model.addAttribute("ss", aa);
		return "add";
	 }
	 
	 
	
	@RequestMapping("selectbrand")
	@ResponseBody
	public List<Tb_brand> selectbrand() {
		List<Tb_brand> list=service.list2();
		return list;
	}
	
	@RequestMapping("selectvariety")
	@ResponseBody
	public List<Tb_goodskind> selectvariety(){
		List<Tb_goodskind> list=service.list3();
		return list;
	}
	
	@RequestMapping("updategood1")
	@ResponseBody
	public Tb_goods updategood1(String id) {
		Tb_goods good=service.updateselect(id);
		System.out.println(good);
		return good;
	}
	
	@RequestMapping("update")
	public String update(Model model,Tb_goods good,MultipartFile myFile,HttpServletRequest request) throws IllegalStateException, IOException {
		if(myFile!=null && myFile.getSize()>0) {
			String real = request.getServletContext().getRealPath("/img/")+"/";
			String ori = myFile.getOriginalFilename();
			ori=UUID.randomUUID().toString()+ori;
			File file = new File(real, ori);
			myFile.transferTo(file);
			good.setTu(ori);
		}
		boolean aa=service.update(good);
		model.addAttribute("ss", aa);
		return "update";
	}
	
	
	@RequestMapping("cheng") 
	public String cheng() {
		return "redirect:/list";
	}
	 
	
	@RequestMapping("chengadd")
	public String chengadd() {
		return "redirect:/list";
	}
	
	@RequestMapping("delete")
	@ResponseBody
	public String delete(Model model,String id) {
		boolean aa=service.delete(id);
		model.addAttribute("aa", aa);
		return "list";
	}
	
	
	
	
}
